package com.example.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.user.entity.User;
import com.example.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangjinlong
 * @version 1.0
 * @date 2021/6/29 10:27
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("list")
    public List<User> list(){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(User::getRegisterDate);
        List<User> list = userService.list(wrapper);
        list.forEach(System.out::println);
        return list;
    }

    @GetMapping("listPage")
    public IPage<User> listPage(){
        IPage<User> iPage = userService.selectUserPage();
        iPage.getRecords().forEach(System.out::println);
        return iPage;
    }

    @GetMapping("get")
    public User get(Long id){
        User user = userService.getById(id);
        System.out.println(user);
        return user;
    }

    @DeleteMapping("delete")
    public Boolean delete(Long id){
        boolean b = userService.removeById(id);
        return b;
    }

    @PostMapping("save")
    public Boolean save(@RequestBody User user){
        boolean save = userService.save(user);
        return save;
    }

    @PostMapping("update")
    public Boolean update(@RequestBody User user){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", user.getId());
        boolean save = userService.update(user, wrapper);
        return save;
    }

    @Value("${info.wl.business.faceInterface.factory:noFace}")
    private String face;

    @GetMapping("face")
    public Boolean getFace(){
        System.out.println("face = " + face);
        if ("noFace".equals(face)) return false;
        if (!StringUtils.hasLength(face)) return false;
        return true;
    }

}
